﻿using System;
using MedicalCare.DbLayer.Adaptors;
using MedicalCare.Messages;
using StructureMap;

namespace MedicalCare.Admin
{
    public partial class StandardValue : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
                InitialEquipmentType();
        }


        protected void rblEquipmentsType_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitialStandardValue();
        }

        private void InitialEquipmentType()
        {
            var equipTypeAdaptor = ObjectFactory.GetInstance<IEquipmentTypeAdaptor>();
            rblEquipmentsType.DataTextField = "Name";
            rblEquipmentsType.DataValueField = "Id";
            rblEquipmentsType.DataSource = equipTypeAdaptor.GetAllEquipmentType();
            rblEquipmentsType.DataBind();
            if (rblEquipmentsType.Items.Count > 0)
                rblEquipmentsType.SelectedIndex = 0;
            InitialStandardValue();
        }

        private void InitialStandardValue()
        {
            if (rblEquipmentsType.Items.Count > 0)
            {
                var index = Convert.ToInt32(rblEquipmentsType.SelectedValue);
                var standrdValueAdaptor = ObjectFactory.GetInstance<IStandardValueAdaptor>();
                var message = new StandardValueMessage()
                                  {
                                      EquipmentTypeId = index
                                  };
                dlStandardValueList.DataSource = standrdValueAdaptor.GetAllStandardValue(message);
                dlStandardValueList.DataBind();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var standrdValueAdaptor = ObjectFactory.GetInstance<IStandardValueAdaptor>();
            var message = new StandardValueMessage()
                              {
                                  EquipmentTypeId = Convert.ToInt32(rblEquipmentsType.SelectedValue),
                                  ItemName = tbItemName.Text,
                                  Value = Convert.ToDecimal(tbItemValue.Text),
                                  Unit = tbUnit.Text
                              };
            var result = standrdValueAdaptor.Update(message);
            if (result > 0)
            {
                spanMsg.InnerHtml = "<script>alert('保存成功！')</script>";
                pnlStandardDetails.Visible = false;
                InitialStandardValue();
            }
            else
            {
                spanMsg.InnerHtml = "<script>alert('保存失败！')</script>";
                pnlStandardDetails.Visible = true;
            }
        }


        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        protected void btnAddItem_Click(object sender, EventArgs e)
        {
            pnlStandardDetails.Visible = true;
            Reset();
        }

        private void Reset()
        {
            spanMsg.InnerHtml = string.Empty;
            tbItemName.Text = string.Empty;
            tbItemValue.Text = string.Empty;
            tbUnit.Text = string.Empty;

        }

        protected void dlStandardValueList_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
        {

        }


    }
}